﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using TSharp.Core.Aop.Handler;

namespace TSharp.Core.Aop.Attributes
{
	/// <summary>
	/// 事务定制特性
	/// </summary>
	public class TransactionAttribute : HandlerAttribute
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="TransactionAttribute"/> class.
		/// </summary>
		/// <param name="dataBaseName">Name of the data base.</param>
		/// <param name="isDistributed">if set to <c>true</c> [is distributed].</param>
		public TransactionAttribute(string dataBaseName, bool isDistributed)
		{
			DataBaseName = dataBaseName;
			IsDistributed = isDistributed;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="TransactionAttribute"/> class.
		/// </summary>
		/// <param name="dataBaseName">Name of the data base.</param>
		public TransactionAttribute(string dataBaseName)
			: this(dataBaseName, false)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="TransactionAttribute"/> class.
		/// </summary>
		public TransactionAttribute()
			: this(string.Empty, false)
		{
			Order = 3;
		}

		/// <summary>
		/// 数据库名称
		/// </summary>
		public string DataBaseName { get; set; }

		/// <summary>
		/// 分布式事务（独立数据库）
		/// </summary>
		public bool IsDistributed { get; set; }

		/// <summary>
		/// Derived classes implement this method. When called, it
		/// creates a new call handler as specified in the attribute
		/// configuration.
		/// </summary>
		/// <param name="container">The <see cref="T:Microsoft.Practices.Unity.IUnityContainer"/> to use when creating handlers,
		/// if necessary.</param>
		/// <returns>A new call handler object.</returns>
		public override ICallHandler CreateHandler(IUnityContainer container)
		{
			return new TransactionHandler(container, this);
		}
	}
}